# -*- coding: utf-8 -*-
# Version: Python 3.9.5
# Author: TRIX
# Date: 2021-09-08 17:33:19
# Use: 给pdf每一页叠加水印 水印格式为pdf 水印只需要一页 保证水印是透明的

import PyPDF3
def add_watermark(pdf_file,watermark_file):
	orifile=open(pdf_file,'rb')
	wmfile=open(watermark_file,'rb')
	origin_pdf=PyPDF3.PdfFileReader(orifile)
	watermark=PyPDF3.PdfFileReader(wmfile)
	new_pdf=PyPDF3.PdfFileWriter()#打开两个pdf 新建一个pdf

	for n in range(origin_pdf.numPages):#每页添加水印
		new_pdf.addPage(origin_pdf.getPage(n))
		new_pdf.getPage(n).mergePage(watermark.getPage(0))
	newfile=open(pdf_file[:-4]+'_watermarked.pdf','wb')
	new_pdf.write(newfile)#写入新建pdf

	orifile.close()
	wmfile.close()
	newfile.close()#关闭三个pdf

add_watermark(r'D:\[CodePrograms]\Python_Primary_Programs\document_processing\pdf_processing\add_watermark\提问的智慧.pdf',r'D:\[CodePrograms]\Python_Primary_Programs\document_processing\pdf_processing\add_watermark\watermark.pdf')
